package com.zhoug.json3demo;

import android.os.Bundle;

import com.google.gson.reflect.TypeToken;
import com.zhoug.json3.GsonUtils;
import com.zhoug.json3.JsonUtils;
import com.zhoug.logger3.Logger;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends MyBaseActivity {
    private static final String TAG = ">>>MainActivity";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.id = "id001";
        user.Name = "张三";
        user.GEnder = "男";
        user.BIRTHDAY = "2001-01-25";
        user.age = 12;
        user.isAdmin = true;
        users.add(user);
        user = new User();
        user.id = "id002";
        user.Name = "李四";
        user.GEnder = "女";
        user.BIRTHDAY = "2001-01-015";
        user.age = 55;
        user.isAdmin = false;
        users.add(user);
        GsonUtils gsonUtils = GsonUtils.getDefault();
        String userJson = gsonUtils.toJson(user);
        String userJsons = gsonUtils.toJson(users);
        Logger.d(TAG,  "########## user toJson ########");
        Logger.d(TAG, userJson);
        Logger.d(TAG,  "########## users toJson ########");
        Logger.d(TAG, userJsons);
        Logger.d(TAG,  "########## user fromJson ########");
        User user1 = gsonUtils.fromJson(userJson, User.class);
        print(user1);
        Logger.d(TAG,  "########## users fromJsonList  ########");
        List<User> users1 = gsonUtils.fromJsonList(userJsons, new TypeToken<List<User>>() {
        });
        print(users1);
        Logger.d(TAG,  "########## users fromJsonArrayList  ########");
        ArrayList<User> users3 = gsonUtils.fromJsonArrayList(userJsons,new TypeToken<ArrayList<User>>() {
        });
        print(users3);
        Logger.d(TAG,  "########## user copy  ########");
        User copy = gsonUtils.copy(user);
        print(copy);
        Logger.d(TAG,  "########## user copyList  ########");
        List<User> copys1 = gsonUtils.copyList(users,new TypeToken<List<User>>(){});
        print(copys1);
        Logger.d(TAG,  "########## user copyArrayList  ########");
        ArrayList<User> copys2 = gsonUtils.copyArrayList(users,new TypeToken<ArrayList<User>>(){});
        print(copys2);

    }


    private <T> void print(List<T> list) {
        if (list == null) {
            Logger.d(TAG, "null");
        } else if (list.isEmpty()) {
            Logger.d(TAG, "[]");
        } else {
            Logger.d(TAG, list.getClass().toString());
            T t = list.get(0);
            Logger.d(TAG, t.getClass() + "->" + JsonUtils.toJson(t));
        }
    }

    private <T> void print(T t) {
        if (t==null){
            Logger.d(TAG, "null");
        }else{
            Logger.d(TAG, t.getClass() + "->" + JsonUtils.toJson(t));
        }
    }


    private static final class User {
        private String id;
        private String Name;
        private String GEnder;
        private String BIRTHDAY;
        private int age;
        private boolean isAdmin;

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", Name='" + Name + '\'' +
                    ", GEnder='" + GEnder + '\'' +
                    ", BIRTHDAY='" + BIRTHDAY + '\'' +
                    ", age=" + age +
                    ", isAdmin=" + isAdmin +
                    '}';
        }
    }

}